package cn.javabb.web.controller;

import cn.javabb.common.exception.ParameterException;
import cn.javabb.common.utils.ResultUtil;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.blog.BlogDO;
import cn.javabb.core.entity.sys.UserAuthDO;
import cn.javabb.core.service.blog.BlogService;
import cn.javabb.core.service.blog.BlogTypeService;
import cn.javabb.core.service.sys.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/22 20:54
 */
@Controller
@RequestMapping("/center")
public class CenterRenderController extends BaseController {
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private BlogTypeService blogTypeService;
    @Autowired
    private BlogService blogService;

    /*博客发布*/
    @GetMapping("/blogPublish")
    public String publish(Model model,Integer blogId){
        if (null != blogId) {
            // 编辑的时候校验参数
            BlogDO blogCheck = new BlogDO().setBlogId(blogId).setUserId(getLoginUser().getId());

            if (blogService.queryCountByWhere(blogCheck) == 0) {
                throw new ParameterException("编辑失败，参数错误");
                // return ResponseModel.Failure("编辑失败，参数错误");
            } else {
                model.addAttribute("blog", blogService.get(blogId));
            }
        }
        //model.addAttribute("catalogId",catalogId);
        model.addAttribute("blogTypeList",blogTypeService.listBlogTypes());
        return "web/view/center/blogPublish";
    }

    /*博客管理*/
    @GetMapping("/blogManage")
    public String blogManage(Model model){
        return "web/view/center/blogManage";
    }
    /*博客评论*/
    @GetMapping("/blogComments")
    public String blogComments(Model model){
        return "web/view/center/blogComments";
    }

    /*账号绑定*/
    @GetMapping("/accountOAuth")
    public ModelAndView accountOAuth(Model model) {
        List<UserAuthDO> userAuthList = userAuthService.listUserAuthByUserId(getLoginUser().getId());
        Map<String, Object> authMap = new HashMap<>();
        userAuthList.forEach(auth -> authMap.put(auth.getAuthType(), auth));
        model.addAttribute("authMap", authMap);
        return ResultUtil.view("web/view/center/accountOAuth");
    }
    /*帖子管理*/
    @GetMapping("/postManage")
    public String postManage(Model model){
        return "web/view/center/postManage";
    }
    /*帖子评论管理*/
    @GetMapping("/postComments")
    public String postComments(Model model){
        return "web/view/center/postComments";
    }
}
